/**
 * 接口权限数据请求
 *
 * @版权所有 rem.studio
 */
import { querySysUrl, deleteSysUrl, addSysUrl, updateSysUrl, exportSysUrl, authorizeRoleToUrl, queryRoleNameCheckbox, queryRoleIdByUrlId } from '@/services/api';

export default {
  namespace: 'sysUrl',

  state: {
    data: {
      list: [],
      pagination: {}
    },
    checkboxData: [],
    checkedRoleData: []
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(querySysUrl, payload);
      yield put({
        type: 'save',
        payload: response
      });
    },
    *fetchCheckbox({ payload }, { call, put }) {
      const response = yield call(queryRoleNameCheckbox, payload);
      yield put({
        type: 'saveCheckbox',
        payload: response
      });
    },
    *fetchCheckedRole({ payload }, { call, put }) {
      const response = yield call(queryRoleIdByUrlId, payload);
      yield put({
        type: 'saveCheckedRole',
        payload: response
      });
    },
    *add({ payload }, { call, put }) {
      const response = yield call(addSysUrl, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *authorize({ payload }, { call, put }) {
      const response = yield call(authorizeRoleToUrl, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *delete({ payload }, { call, put }) {
      const response = yield call(deleteSysUrl, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *update({ payload }, { call, put }) {
      const response = yield call(updateSysUrl, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *export({ payload }, { call, put }) {
      const response = yield call(exportSysUrl, payload);
      yield put({
        type: 'save',
        payload: response
      });
      return response;
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data
      };
    },
    saveCheckbox(state, action) {
      return {
        ...state,
        checkboxData: action.payload.data
      };
    },
    saveCheckedRole(state, action) {
      return {
        ...state,
        checkedRoleData: action.payload.data
      };
    }
  }
};
